/*
 * @Author: 何丽彬
 * @Email: helibin@yingzi.com
 * @Date: 2021-11-18 11:18:05
 * @LastEditTime: 2021-11-18 11:21:02
 * @LastEditors: 何丽彬
 * @FilePath: \process-learn\demo1.js
 */
const http = require("http");
const longComputation = () => {
  let sum = 0;
  for (let i = 0; i < 1e10; i++) {
    sum += i;
  }
  return sum;
};
const server = http.createServer();
server.on("request", (req, res) => {
  if (req.url === "/compute") {
    console.time("计算耗时");
    console.info("计算开始", new Date());
    const sum = longComputation();
    console.info("计算结束", new Date());
    console.timeEnd("计算耗时");
    return res.end(`Sum is ${sum}`);
  } else {
    res.end("Ok");
  }
});

server.listen(3000);
//打印结果
//计算开始 2019-07-28T07:08:49.849Z
//计算结束 2019-07-28T07:09:04.522Z
